package com.xinhe.web.selfpay;
import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.app.model.*;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.model.Transaction;
import java.net.URL;
/** AppService使用示例 */
public class AppTest {

  /** 商户号 */
  public static String merchantId = "1616713201";
  /** 商户API私钥路径,这里写的是本地路径，线上的话要换成docker容器内的证书路径（volumns映射）*/
  //public static String privateKeyPath = "D:/zhanghou/apiclient_key.pem";
  //public static String privateKeyPath = "classpath:cert/apiclient_key.pem";
  /** 商户证书序列号 */
  public static String merchantSerialNumber = "57906AE4C493222D0DDD03D81A3A2F2AA93B8D19";
  /** 商户APIV3密钥 */
  public static String apiV3Key = "12345678901234567890ABCDEFGHIJKL";
  public static AppService service;

  public static void main(String[] args) {
    String privateKeyPath = "";
    URL resource = AppTest.class.getClassLoader().getResource("cert/apiclient_key.pem");
    if (resource != null) {
      privateKeyPath = resource.getPath();
      System.out.println("文件路径：" + privateKeyPath);
    } else {
      System.out.println("找不到指定的文件");
    }
    // 初始化商户配置
    Config config =
        new RSAAutoCertificateConfig.Builder()
            .merchantId(merchantId)
            // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
            .privateKeyFromPath(privateKeyPath)
            .merchantSerialNumber(merchantSerialNumber)
            .apiV3Key(apiV3Key)
            .build();
    // 初始化服务
    service = new AppService.Builder().config(config).build();
    // ... 调用接口
      prepay();
  }
  /** 关闭订单 */
  public static void closeOrder() {
    CloseOrderRequest request = new CloseOrderRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    service.closeOrder(request);
  }
  /** APP支付下单 */
  public static void prepay() {
    PrepayRequest request = new PrepayRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    Amount amount = new Amount();
    amount.setTotal(1);
    amount.setCurrency("CNY");
    request.setAmount(amount);
    request.setAppid("wx2425d0af98810b00");
    request.setMchid("1616713201");
    request.setDescription("测试商品999");
    request.setNotifyUrl("http://1941w5190a.yicp.fun:49760/test/acceptWeixin");
    request.setOutTradeNo("out_trade_no_999");
    Payer payer = new Payer();
    payer.setOpenid("olYZb5aMoVTziZtD8MYK5dIV8TmE");
    // 调用接口
    PrepayResponse prepay = service.prepay(request);
    System.out.println("哈哈哈："+ JSONObject.toJSONString(prepay));
  }
  /** 微信支付订单号查询订单 */
  public static Transaction queryOrderById() {
    QueryOrderByIdRequest request = new QueryOrderByIdRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    return service.queryOrderById(request);
  }
  /** 商户订单号查询订单 */
  public static Transaction queryOrderByOutTradeNo() {
    QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
    // 调用request.setXxx(val)设置所需参数，具体参数可见Request定义
    // 调用接口
    return service.queryOrderByOutTradeNo(request);
  }



}
